#!/bin/bash


function print_usage()
{

  echo "
Usage:	$(basename $0) zonename catagory [zonetype [masters/forwarders [-f]]]

	\"catagory\" must be one of \"its\", \"base\", \"mp\", \"qa\", \"misc\" and \"nodb\".

	\"zonetype\" could be \"master\", \"slave\", \"forward\", or, \"stub\".

	-f: generate configuration even if the zone is configured

"

}

function print_master()
{

zone=$1
type=$2
# masters=$3
db=$3

  printf "zone \"%s\" {\n" $zone
  printf "\ttype %s;\n" $type
  printf "\tfile \"%s\";\n};\n\n" $db
  return 0

}

function print_slave()
{

zone=$1
type=$2
masters="$3"
db=$4

  if [ "X${masters}" == "X" ]; then
	echo -e "\"masters\" cannot be empty for the catagory \"$catagory\"." >&2
	return 1
  fi

  printf "zone \"%s\" {\n" $zone
  printf "\ttype %s;\n" $type
  printf "\tmasters { "

  for server in $(echo $masters); do 
        printf "%s; " $server
  done

  printf "};\n";
  printf "\tfile \"%s\";\n};\n\n" $db
  return 0

}

function print_foward()
{

zone=$1
type=$2
masters="$3"

  if [ "X${masters}" == "X" ]; then
	echo -e "\"forwarders\" cannot be empty for the catagory \"$catagory\"." >&2
	return 1
  fi

  printf "zone \"%s\" {\n" $zone
  printf "\ttype %s;\n" $type
  printf "\tforwarders { "

  for server in $(echo $masters); do 
        printf "%s; " $server
  done

  printf "};\n};\n\n";
  return 0

}

# ==================== start of MAIN ============================

ROOT=""
CONF_DIR="${ROOT}/etc/named"
ZONE="/var/tmp/ZONECAT"
DIR=$(dirname $ZONE)
FILE=$(basename $ZONE)


export PATH=/usr/bin:/usr/sbin

if [ $# -lt 2 -o $# -gt 5 ]; then
	print_usage
	exit 1
fi

zone=$1
catagory=$2
type=$3
type=${type:="master"}
masters=$4
force="$5"

if [ "X$(uname -s)" == "XSunOS" ]; then
    GREP="/usr/xpg4/bin/grep"
    SED="/usr/xpg4/bin/sed"
else
    GREP="/bin/grep"
    SED="/bin/sed"
fi

case "$catagory" in
	'its')
		masters=${masters:="10.58.10.16/10.58.10.15"}
		;;
	'mp')
		masters=${masters:="10.153.43.20/10.161.119.20"}
		;;
	'qa')
		masters=${masters:="10.153.43.20/10.161.119.20"}
		;;
	'base' | 'misc' | 'nodb' | 'MINE' )
		masters=$4
		;;
	*)
		echo -e "\nInvalid catagory \"${catagory}\"." >&2
		print_usage
		exit 1;;
esac
masters=$(echo $masters | sed -e 's/\// /g')

catagory_zones="${CONF_DIR}/${catagory}.zones"
excluded_zones="${CONF_DIR}/excluded/${catagory}.zones"

if [ "X$(find $DIR -name $FILE -mmin -10)" == "X" ]; then
	/usr/local/sbin/zonecat -T > $ZONE
fi

#if $GREP -q "^[[:space:]]*zone[[:space:]]\{1,\}\"${zone}\"" ${CONF_DIR}/*.zones; then
if awk '{print $1}' $ZONE | $GREP -q "^${zone}$"; then 
	echo -e "\nWarning: Zone \"${zone}\" has been configured on" $($GREP "^${zone}[[:space:]]" $ZONE | awk '{print $4 }' | cut -d"/" -f1)".zones\n" >&2
	if [ "X${force}" != "X-f" ]; then exit 2; fi
fi

# if /usr/xpg4/bin/grep -q "^[[:space:]]*${zone}[[:space:]]*$" ${CONF_DIR}/excluded/*.zones; then
# 	echo -e "\nZone \"${zone}\" has been defined in configuration file: \c" >&2
# 	/usr/xpg4/bin/grep "^[[:space:]]*${zone}[[:space:]]*$" ${CONF_DIR}/excluded/*.zones | awk -F":" '{print $1 ".\n" }' >&2
# 	exit 2
# fi


echo "$zone" | $GREP -q ".in-addr.arpa$"
if [ $? -eq 0 ]; then
	db="";
	for i in $(echo "$zone" | sed -e 's/\.in-addr\.arpa//' -e 's/\./ /g'); do
		if [ "X$db" != "X" ]; then db="${i}.${db}"; else db=$i; fi
		#echo "db=$db		i=$i"
	done
	db="$catagory/named.rev-${db}"
else
	db="$catagory/db.${zone}"
fi

case $type in
	'master')
		print_master $zone "master" $db
		;;
	'slave')
		print_slave $zone "slave" "$masters" $db
		;;
	'forward')
		print_foward $zone "forward" "$masters"
		;;
	'stub')
		print_slave $zone "stub" "$masters" $db
		;;
	*)
		echo -e "\nInvalid zone type \"$type\"." >&2
		print_usage
		exit 3;;
esac


if [ $? -ne 0 ]; then
	print_usage
	exit 3
fi


# ==================== end of MAIN ============================
